﻿using Newtonsoft.Json;
using SenFangKaoHe_MVC.Models.Entity;
using SenFangKaoHe_MVC.Models.ViewModel;
using SqlSugar;
using SqlSugar.SplitTableExtensions;
using System.Diagnostics;

namespace SenFangKaoHe_MVC.Services
{
    public class KaoShiServices
    {
        private readonly SqlSugarClient db;
        public KaoShiServices(SqlSugarClient db) {
            this.db = db;
        }

        public async Task<List<tikukaoti>> getKaoTi(int tkid)
        {
            return await db.Queryable<tikukaoti>()
                .Where(kt => kt.tikuid == tkid)
                .OrderBy(kt => kt.tixing)
                .ToListAsync();
        }

        public async Task<List<tikukaoti>> getKaoTi(int[] tkids, int[] txs, int[] tls)
        {
            List<tikukaoti> tkkts = new List<tikukaoti>();
            //题型
            for (int i = 0; i < txs.Length; i++)
            {
                var _tx = txs[i];
                var _tl = tls[i];
                var wherestring = "";
                foreach(int a in tkids)
                {
                    wherestring += " tikuid = " + a + " or ";
                }
                wherestring = wherestring.Substring(0, wherestring.Length - 4);
                var sql = "select top "+_tl+ " * from tikukaoti where "+wherestring+" and tixing = "+_tx+" order by newid()";
                List<tikukaoti> _tkkts = await db.SqlQueryable<tikukaoti>(sql)
                    .ToListAsync();
                tkkts.AddRange(_tkkts);
            }
            return tkkts;
        }


        public async Task<List<TempShiJuanKaoTi>> getShiJuanKaoTi(int[] tkids, int[] txs, int[] tls, int[] fzs)
        {
            List<TempShiJuanKaoTi> tkkts = new List<TempShiJuanKaoTi>();
            List<tikukaoti> kts = new List<tikukaoti>();
            //题型
            for (int i = 0; i < txs.Length; i++)
            {
                var _tx = txs[i];
                var _tl = tls[i];
                var _fz = fzs[i];
                var wherestring = "";
                foreach (int a in tkids)
                {
                    wherestring += " tikuid = " + a + " or ";
                }
                wherestring = wherestring.Substring(0, wherestring.Length - 4);
                //var sql = "select top " + _tl + " * from tikukaoti where " + wherestring + " and tixing = " + _tx + " order by newid()";
                var sql = "select top " + _tl + " * from (select  * from tikukaoti where " + wherestring + ") as a where a.tixing = " + _tx + " order by newid()";
                List<tikukaoti> _tkkts = await db.SqlQueryable<tikukaoti>(sql)
                    .ToListAsync();
                foreach (tikukaoti kt in _tkkts)
                {
                    TempShiJuanKaoTi pkt = new TempShiJuanKaoTi();
                    pkt.TiMu = kt.timu;
                    pkt.KaoTiId = kt.Id;
                    pkt.TiXingId = kt.tixing;
                    List<String> xslist = new List<string>();
                    for(var x = 0; x < kt.xuanxiang.Split(";").Length; x++)
                    {
                        xslist.Add(kt.xuanxiang.Split(";")[x]);
                    }
                    pkt.XuanXiang = xslist;
                    pkt.DuiYuanDaAn = "";
                    pkt.FenZhi = _fz;
                    pkt.BiaoJi = true;
                    pkt.BiaoZhunDaAn = kt.daan;
                    tkkts.Add(pkt);
                }
            }
            Debug.WriteLine("获取的试题总数量："+tkkts.Count);
            return tkkts;
        }

        public async Task<List<TempShiJuanKaoTi>> getMoNiKaoTi(int tkid, int[] txs, int[] tls, int[] fzs)
        {
            List<TempShiJuanKaoTi> tkkts = new List<TempShiJuanKaoTi>();
            List<tikukaoti> kts = new List<tikukaoti>();
            //题型
            for (int i = 0; i < txs.Length; i++)
            {
                var _tx = txs[i];
                var _tl = tls[i];
                var _fz = fzs[i];
                var wherestring = " tikuid = " + tkid;
                var sql = "select top " + _tl + " * from (select  * from tikukaoti where " + wherestring + ") as a where a.tixing = " + _tx + " order by newid()";
                List<tikukaoti> _tkkts = await db.SqlQueryable<tikukaoti>(sql)
                    .ToListAsync();
                foreach (tikukaoti kt in _tkkts)
                {
                    TempShiJuanKaoTi pkt = new TempShiJuanKaoTi();
                    pkt.TiMu = kt.timu;
                    pkt.KaoTiId = kt.Id;
                    pkt.TiXingId = kt.tixing;
                    List<String> xslist = new List<string>();
                    for (var x = 0; x < kt.xuanxiang.Split(";").Length; x++)
                    {
                        xslist.Add(kt.xuanxiang.Split(";")[x]);
                    }
                    pkt.XuanXiang = xslist;
                    pkt.DuiYuanDaAn = "";
                    pkt.FenZhi = _fz;
                    pkt.BiaoJi = true;
                    pkt.BiaoZhunDaAn = kt.daan;
                    tkkts.Add(pkt);
                }
            }
            Debug.WriteLine("获取的试题总数量：" + tkkts.Count);
            return tkkts;
        }

        public async Task<kaohe> getKaoHe(int khid)
        {
            return await db.Queryable<kaohe>()
                .Includes(kh => kh.shijuans)
                .Where(kh => kh.Id == khid)
                .SingleAsync();
        }

        public async Task<List<kaohe>> getKaoHes()
        {
            List<kaohe> khs = await db.Queryable<kaohe>()
                .Includes(kh => kh.shijuans)
                .Where(kh => kh.RiQi >= DateTime.Now && kh.RiQi <= DateTime.Now.AddDays(7))
                .ToListAsync();
            return khs;
        }

        public async Task<List<kaohe>> getKaoHes(string xm)
        {
            List<kaohe> khs = await db.Queryable<kaohe>()
                .Includes(kh => kh.shijuans)
                .Where(kh => kh.RiQi >= DateTime.Now && kh.RiQi <= DateTime.Now.AddDays(7))
                .ToListAsync();
            for (int i = 0; i < khs.Count;i++)
            {
                shijuan sj = await getShiJuan(khs[i].Id, xm);
                if (sj != null)
                {
                    khs[i].shijuans.Clear();
                    khs[i].shijuans.Add(sj);
                }
                else
                {
                    khs[i].shijuans.Clear();
                }
            }
            return khs;
        }

        public async Task<List<kaohe>> getWeiKaoHes(string xm)
        {
            List<kaohe> khs = await db.Queryable<kaohe>()
                .Where(kh => kh.RiQi >= DateTime.Now && kh.RiQi <= DateTime.Now.AddDays(7))
                .ToListAsync();
            for(int i = 0;i<khs.Count;)
            {
                shijuan sj = await getShiJuan(khs[i].Id, xm);
                if(sj != null)
                {
                    khs.Remove(khs[i]);
                }
                else
                {
                    i++;
                }
            }
            return khs;

        }

        public async Task<int> saveShiJuan(shijuan _shijuan)
        {
            int sjid = await db.Insertable(_shijuan).ExecuteReturnIdentityAsync();
            return sjid;
        }

        public async Task<shijuan> getShiJuan(int sjid)
        {
            return await db.Queryable<shijuan>()
                .Includes(sj => sj.kaohe)
                .Where(sj => sj.Id == sjid)
                .SingleAsync();
        }

        public async Task<shijuan> getShiJuan(int khid,string xm)
        {
            //判断人员是否参加过此次考核
            return await db.Queryable<shijuan>()
                .Includes(sj => sj.kaohe)
                .Where(sj => sj.kaoheid == khid && sj.xingming == xm)
                .SingleAsync();
        }

        public async Task<TempShiJuan> getCuoTis(int sjid)
        {
            TempShiJuan sjct = new TempShiJuan();

            shijuan sj = await getShiJuan(sjid);
            Debug.WriteLine(JsonConvert.SerializeObject(sj));
            string tkid = sj.kaohe.TiKuid;
            string khfw = await new TiKuServices(db).getTiKuMingCheng(tkid);
            sjct.Id = sj.Id;
            sjct.XingMing = sj.xingming;
            sjct.KaoHeMingCheng = sj.kaohe.MingCheng;
            sjct.KaoHeFanWei = khfw;
            sjct.FenZhi = sj.fenzhi;
            sjct.ChengJi = sj.chengji;

            int[] ctid = new int[sj.cuoti_ids.Split(",").Length];
            string[] ctda = new string[sj.cuoti_das.Split(",").Length];
            for(int t = 0; t < ctid.Length; t++)
            {
                ctid[t] = Convert.ToInt32(sj.cuoti_ids.Split(",")[t]);
            }
            for (int t = 0; t < ctda.Length; t++)
            {
                ctda[t] = sj.cuoti_das.Split(",")[t];
            }

            List<TempShiJuanKaoTi> cts = new List<TempShiJuanKaoTi>();
            for (int i = 0; i < ctid.Length; i++)
            {
                List<string> xx = new List<string>();
                tikukaoti st = await new TiKuServices(db).getTiKuKaoTi(Convert.ToInt32(ctid[i]));
                for(int a = 0; a < st.xuanxiang.Split(';').Length; a++)
                {
                    xx.Add(st.xuanxiang.Split(";")[a]);
                }
                int fz = 0;
                switch (st.tixing)
                {
                    case 1:
                        fz = Convert.ToInt32(sj.kaohe.TianKongFen);
                        break;
                    case 2:
                        fz = Convert.ToInt32(sj.kaohe.DanXuanFen);
                        break;
                    case 3:
                        fz = Convert.ToInt32(sj.kaohe.DuoXuanFen);
                        break;
                    case 4:
                        fz = Convert.ToInt32(sj.kaohe.PanDuanFen);
                        break;
                }
                TempShiJuanKaoTi ct = new TempShiJuanKaoTi();
                ct.Id = i + 1;
                ct.TiMu = st.timu;
                ct.XuanXiang = xx;
                ct.BiaoZhunDaAn = st.daan;
                ct.DuiYuanDaAn = ctda[i];
                ct.TiXingId = st.tixing;
                ct.FenZhi = fz;
                cts.Add(ct);
            }
            sjct.ShiTi = cts;


            return sjct;
        }
    }
}
